import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        websock: null,
        eventlist:[]
    },
    getters: {
        onEvent(state) {
            return function (method) {
                let index = state.eventlist.map((eb) => {return eb.method}).indexOf(method);
                if (state.eventlist.length > 0 && index >= 0) {
                    let result = Object.assign({}, state.eventlist[index]);
                    state.eventlist.splice(index, 1);
                    return result.data;
                }
                return null;
            }
        }
    },
    mutations: {
        WEBSOCKET_INIT(state,url) {
            state.websock = new WebSocket(url);
            state.websock.onopen = function () {
                console.log("连接成功！");
            }
            state.websock.onmessage = function (callBack) {
                console.log("ws接收！");
                console.log(callBack.data);

                var dataStr = ArrayBuffer2String(callBack.data);

                console.log(" dataStr:" + dataStr)

                var res = JSON.parse(dataStr)

                if (res.module === "Heart") {
                    return
                }
                state.eventlist.push({
                    method:res.module,
                    data:res
                })
            }
            state.websock.onerror=function(e) { //错误
                console.log("ws错误!");
                console.log(e);
            }
            state.websock.onclose=function(e) { //关闭
                console.log("ws关闭！");
                console.log(e);
            }
            state.websock.binaryType = "arraybuffer";
            //发送心跳包
            setInterval(function() {
                console.log("ws发送心跳！");
                var heart={
                    id:"Heart",
                    body:{}
                };
                state.websock.send(makeByte(heart));
            }, 30000)
        },
        WEBSOCKET_SEND(state, p) {
            console.log("ws发送！");
            state.websock.send(makeByte(p));
        }
    },
    actions: {
        WEBSOCKET_INIT({commit},url) {
            commit('WEBSOCKET_INIT',url)
        },
        WEBSOCKET_SEND({commit}, p) {
            //p.type=3;
            commit('WEBSOCKET_SEND', p)
        }
    }
})

function makeByte(p){
    var h = {
        id:p.id,
        body: JSON.stringify(p.body)
    }
    return JSON.stringify(h);
}
function String2Buffer(str) {
    var bytes = new Array();
    var len, c;
    len = str.length;
    for (var i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if (c >= 0x010000 && c <= 0x10FFFF) {
            bytes.push(((c >> 18) & 0x07) | 0xF0);
            bytes.push(((c >> 12) & 0x3F) | 0x80);
            bytes.push(((c >> 6) & 0x3F) | 0x80);
            bytes.push((c & 0x3F) | 0x80);
        } else if (c >= 0x000800 && c <= 0x00FFFF) {
            bytes.push(((c >> 12) & 0x0F) | 0xE0);
            bytes.push(((c >> 6) & 0x3F) | 0x80);
            bytes.push((c & 0x3F) | 0x80);
        } else if (c >= 0x000080 && c <= 0x0007FF) {
            bytes.push(((c >> 6) & 0x1F) | 0xC0);
            bytes.push((c & 0x3F) | 0x80);
        } else {
            bytes.push(c & 0xFF);
        }
    }
    return bytes;
}

function String2ArrayBuffer(str) {
    return Buffer2ArrayBuffer(String2Buffer(str))
}

function ArrayBuffer2Buffer(arrbuff) {
    var buff = new Buffer(arrbuff.byteLength);
    var view = new Uint8Array(arrbuff);
    for (var i = 0; i < buff.length; ++i) {
        buff[i] = view[i];
    }
    return buff;
}

function Buffer2ArrayBuffer(buff) {
    var arrbuff = new ArrayBuffer(buff.length);
    var view = new Uint8Array(arrbuff);
    for (var i = 0; i < buff.length; ++i) {
        view[i] = buff[i];
    }
    return arrbuff;
}

function Buffer2String(arr) {
    var str = '';
    for (var i = 0; i < arr.length; i++) {
        str += String.fromCharCode(arr[i]);
    }
    return str
}

function ArrayBuffer2String(arr) {
    return Buffer2String(ArrayBuffer2Buffer(arr))
}